package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.ApiAdminService;
import com.platform.service.ApiCategoryService;
import com.platform.service.ApiShopService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "基础信息相关")
@RestController
@RequestMapping("/api/catalog")
public class ApiCatalogController extends ApiBaseAction {
    @Autowired
    private ApiCategoryService categoryService;
    @Autowired
    private ApiShopService apiShopService;
    @Autowired
    private ApiAdminService apiAdminService;

    /**
     * 查询供应商商品分类
     */
    @ApiOperation(value = "查询供应商商品分类")
    @IgnoreAuth
    @PostMapping(value = "index")
    public Object index() {
        Map params = new HashMap();
        params.put("sidx", "sort_order");
        params.put("order", "asc");
        params.put("parent_id", 0);
        //查询列表数据
        List<CategoryVo> data = categoryService.queryList(params);
        CategoryVo currentCategory = null;

        if (null == currentCategory && null != data && data.size() != 0) {
            currentCategory = data.get(0);
        } else {
            currentCategory = new CategoryVo();
        }

        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            params.put("parent_id", currentCategory.getId());
            currentCategory.setSubCategoryList(categoryService.queryList(params));
        }
        return toResponsSuccess(data);
    }

    /**
     * 查询商铺商品分类
     */
    @ApiOperation(value = "查询商铺商品分类")
    @IgnoreAuth
    @PostMapping(value = "shop/index")
    public Object shopIndex() {
        JSONObject json = this.getJsonRequest();
        Map<String,Object> map = JSONObject.toJavaObject(json,Map.class);
        Integer shopId = (Integer) map.get("shopId");
        List<CategoryVo> result = categoryService.queryShopList(shopId);
        return toResponsSuccess(result);
    }

    /**
     * 查询用户商铺商品分类
     */
    @ApiOperation(value = "查询商铺商品分类")
    @PostMapping(value = "shop/user/index")
    public Object shopUserIndex(@LoginUser UserVo loginUser) {
        //根据userId查询该用户的shopId
        ShopVo vo = apiShopService.queryByUserId(loginUser.getUserId().intValue());
        List<CategoryVo> result = categoryService.queryShopList(vo.getShopId());
        return toResponsSuccess(result);
    }


    /**
     * 查询用平台信息
     */
    @ApiOperation(value = "查询用平台信息")
    @IgnoreAuth
    @PostMapping(value = "queryAdminInfo")
    public Object queryAdminInfo() {
        List<AdminInfoDto> result = apiAdminService.queryList();
        return toResponsSuccess(result);
    }

    /**
     * 查询轮播图
     */
    @ApiOperation(value = "查询轮播图")
    @IgnoreAuth
    @PostMapping(value = "queryAd")
    public Object queryAd() {
        List<AdVo> result = apiAdminService.queryAd();
        return toResponsSuccess(result);
    }

}